import { getList, findSimCardbyid, simCardAdd, bindingdevice, simCardEdit } from '../services/simCard';

export default {
  namespace: 'simCard',
  state: {
    list: {
      list: [],
      pagination: {},
    },
    loading: false,
  },
  effects: {
    *findById({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findSimCardbyid, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getList, payload);
      const pageData = {
        list: response.data?.list ?? [],
        pagination: response.data?.pagination ??
          { current: 1, pageSize: 10, total: 0 }
      }
      yield put({
        type: 'appendList',
        payload: pageData,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(simCardAdd, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(simCardEdit, payload);
      yield put({
        type: 'save',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindingDevice({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindingdevice, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *clearData({ }, { put }) {
      yield put({
        type: 'changeLoading',
        payload: true
      });
      yield put({
        type: 'appendList',
        payload: {
          list: [],
          pagination: {
            current: 0,
            total: 0
          },
          rows: [],
        }
      });
      yield put({
        type: 'changeLoading',
        payload: false
      });
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  },
};
